﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="DHGMS Solutions" file="Zip.cs">
//   Copyright 2004-2014 DHGMS Solutions.
//      
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//      
//   http://www.apache.org/licenses/LICENSE-2.0
//      
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// <summary>
//   
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Dhgms.JobHelper.Tasks
{
    using System;
    using System.Diagnostics.Contracts;
    using System.Threading.Tasks;

    using Dhgms.TplHelper.Model.Info.TaskResult;
    using Dhgms.TplHelper.Tasks;

    using Ionic.Zip;
    using Ionic.Zlib;

    /// <summary>
    /// Task for compressing a file into a zip file
    /// </summary>
    public class Zip : TaskBase<RecordProcessor>
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="Zip"/> class. 
        /// </summary>
        /// <param name="sourceFullFileName">
        /// File we're tranferring
        /// </param>
        /// <param name="targetFileName">
        /// Name the file will be stored as (will overwrite if it already exists)
        /// </param>
        public Zip(string sourceFullFileName, string targetFileName)
            : base("Compressing File with zip")
        {
            Contract.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(sourceFullFileName), "sourceFullFileName");
            Contract.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(targetFileName), "targetFileName");

            this.SourceFullFileName = sourceFullFileName;
            this.TargetFileName = targetFileName;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the name of the file we're tranferring
        /// </summary>
        private string SourceFullFileName { get; set; }

        /// <summary>
        /// Gets or sets the name of the file will be stored as (will overwrite if it already exists)
        /// </summary>
        private string TargetFileName { get; set; }

        #endregion

        #region Methods

        protected override string Reference
        {
            get
            {
                return this.SourceFullFileName + " -> " + this.TargetFileName;
            }
        }

        /// <summary>
        /// The OnProcess event for doing the donkey work
        /// </summary>
        /// <param name="result">
        /// object that contains result information
        /// </param>
        protected override async Task OnProcess(RecordProcessor result)
        {
            await TaskEx.Run(() =>
            {
                using (var zip = new ZipFile())
                {
                    // removes folders from the zip file, so everything is in the root
                    zip.AddFile(this.SourceFullFileName, string.Empty);
                    zip.CompressionLevel = CompressionLevel.BestCompression;
                    zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
                    zip.Save(this.TargetFileName);
                }                
            }
                );

            result.RecordsProcessed = 1;
        }

        #endregion
    }
}